package com.lilanz.facepass.config;


import com.lilanz.facepass.entity.exception.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * @ Description：全局异常处理，把错误的信息返回给前端，并且把错误的信息插入数据库中
 */
@RestControllerAdvice
public class ExceptionConfig {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    // 最大的异常处理,所有的Exception的异常都在这里,并返回
    @ExceptionHandler(Exception.class)
    public Map<String, Object> Exception(HttpServletRequest request, Exception e) {
        Map<String, Object> map = new HashMap<>();
        map.put("errcode", -1);
        map.put("errmsg", "出错了："+e.getMessage());
        return map;
    }

    // 自定义异常处理
    @ExceptionHandler(MyException.class)
    public Map<String, Object> myException(HttpServletRequest request, MyException e) {
        // 返回给前端错误的信息
        Map<String, Object> map = new HashMap<>();
        map.put("errcode", -1);
        map.put("errmsg","出错了："+e.getMessage());
        return map;
    }
}
